'************************************************************************************
' Copyright © 2003-2005 Kathleen Dollard
'
' This software is provided 'as-is', without any express or implied warranty. In no 
' event will the author be held liable for any damages arising from the use of this
' software.
' 
' You may use this software for any purpose, including commercial applications, 
' and you may redistribute this software as part of application delivery: however, 
' this copyright notice may not be removed and you may not otherwise redistribute 
' this software directly other than providing a link to wwww.gendotnet.com. 
'
' You may alter this software to better server your needs (I'd like to hear about your 
' changes in order to make the software better). If you alter the softwre, you must  
' clearly mark it as altered and must not be misrepresented as the original software.
' nor wholly your original work.
'*************************************************************************************
Option Strict On
Option Explicit On 

Imports System

Namespace KADGen.CodeGenerationSupport
   Public Class CompareXMLFiles

#Region "Public properties and methods"
      Public Sub Run( _
                     ByVal node As Xml.XmlNode, _
                     ByVal runUtility As RunUtility, _
                     ByVal statusDirective As IStatusDirective)
         Dim statusoutput As IStatusOutput
         Dim compareXMLFileDirective As CodeGenerationSupport.ICompareXMLFileDirective
         Dim outputFile As String
         Dim compareXMLFiles As New Utility.CompareXMLFiles
         Dim firstFileName As String
         Dim secondFileName As String
         Dim firstXMLDoc As New Xml.XmlDocument
         Dim secondXMLDoc As New Xml.XmlDocument
         Dim knownChangesXMLDoc As New Xml.XmlDocument
         Dim logXMLDoc As New Xml.XmlDocument
         Dim rootLogNode As Xml.XmlNode
         compareXMLFileDirective = CType(node, CodeGenerationSupport.ICompareXMLFileDirective)
         outputFile = compareXMLFileDirective.SinglePass.OutputFile
         statusoutput = statusDirective.AddStatusOutput( _
                  outputFile, _
                  FileTreatment.Unknown)
         Try
            knownChangesXMLDoc = KnownChanges(compareXMLFileDirective, statusoutput)
            firstFileName = compareXMLFileDirective.LeftXMLFileName
            secondFileName = compareXMLFileDirective.RightXMLFileName
            If firstFileName.Trim.Length = 0 Or _
                     secondFileName.Trim.Length = 0 Or _
                     outputFile.Trim.Length = 0 Then
               ' KADLOG: missing name
            ElseIf Not IO.File.Exists(firstFileName) Or _
                     Not IO.File.Exists(secondFileName) Then
               ' KADLOG: missing file
            Else
               firstXMLDoc.Load(firstFileName)
               secondXMLDoc.Load(secondFileName)
               rootLogNode = Utility.xmlHelpers.NewElement(logXMLDoc, "Changes")
               logXMLDoc.AppendChild(rootLogNode)
               compareXMLFiles.CompareXMLDocs(rootLogNode, firstXMLDoc, secondXMLDoc, knownChangesXMLDoc)
               logXMLDoc.Save(outputFile)
            End If
         Catch ex As System.Exception
            statusoutput.AddStatusException("CreateMetadata.Run", "Failure creating file", ex)
         End Try
         statusoutput.Status = statusoutput.ChildStatus
      End Sub
#End Region

#Region "Private properies and methods"
      Private Function KnownChanges( _
               ByVal compareXMLFilesDirective As CodeGenerationSupport.ICompareXMLFileDirective, _
               ByVal statusOutput As CodeGenerationSupport.IStatusOutput) _
               As Xml.XmlDocument
         Dim xsd As Xml.Schema.XmlSchema
         Dim morphType As System.Type = GetType(KADGen.MorphMetadata.Singletons)
         Dim knownChangesStream As IO.Stream = Nothing
         Dim knownChangesFileName As String
         Dim knownChangesValidator As Utility.XmlValidator
         Dim ret As Xml.XmlDocument
         If compareXMLFilesDirective.IgnoreKnownChanges Then
            xsd = Utility.Tools.GetEmbeddedXsd(morphType, "KADGenKnownChanges.xsd")
            knownChangesFileName = compareXMLFilesDirective.KnownChangesFile
            Try
               If knownChangesFileName.Trim.Length > 0 Then
                  knownChangesStream = New IO.FileStream( _
                           knownChangesFileName, IO.FileMode.Open)
                  ' KADLOG: Add logging if not found
               Else
                  ' use the internal one
                  knownChangesStream = Utility.Tools.GetStreamFromStringResource( _
                              morphType, "KnownMetadataChanges.xml")
               End If
               knownChangesValidator = New Utility.XmlValidator(knownChangesStream, xsd)
               ret = knownChangesValidator.LoadAndValidate()
            Finally
               If knownChangesStream IsNot Nothing Then
                  knownChangesStream.Close()
               End If
            End Try
         Else
            ret = New Xml.XmlDocument
         End If
         Return ret
      End Function
#End Region
   End Class
End Namespace